﻿using System;

namespace SimpleServiceBus.Bus.Pipeline.PipelineComponents
{
    public abstract class PipelineComponentBase : IMessagePipelineComponent
    {
        protected PipelineComponentBase()
        {
            Name = GetType().Name;
            Enabled = true;
        }

        protected PipelineComponentBase(string name)
        {
            Name = name;
            Enabled = true;
        }

        #region IMessagePipelineComponent Members

        public IMessagePipeline Pipeline { get; set; }

        public string Name { get; set; }

        public bool Enabled { get; set; }

        public bool EnabledFor(MessageEnvelope messageEnvelope)
        {
            return Enabled;
        }

        public void ProcessMessage(MessageEnvelope messageEnvelope)
        {
            if (Pipeline == null)
                throw new InvalidOperationException(
                    "ProcessMessage cannot be called on a PipelineComponent unless the Pipeline property has been initialized with the current Pipeline executing the component.");

            ProcessMessage(messageEnvelope, Pipeline.CurrentContext);
        }

        public virtual void Dispose()
        {
            Pipeline = null;
        }

        #endregion

        protected abstract void ProcessMessage(MessageEnvelope messageEnvelope, IMessageContext context);
    }
}